const express = require('express');
const app = express();
const {
  resolve
} = require('path');
const {
  readFileSync,
  read
} = require('fs');

app.all('*', (req, res, next) => {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-methods', 'POST,GET');
  next();
});

//localhost:3000/images/1.jpg
app.get('/images/:filename', (req, res) => {
  res.sendFile(resolve(__dirname, './images/' + req.params.filename));
});

//localhost:3000/imgs
app.get('/imgs', (req, res) => {
  const imageData = JSON.parse(readFileSync(resolve(__dirname, './data/images.json'), 'utf8'));

  res.send(imageData);
});

app.listen(3000, () => {
  console.log('welcome to use Express on 3000!');
});